﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Orid.Management.Statistics.Tools.Decomposition.Model;

using System.Data.SqlClient;
using Orid.Data.SqlServer;
using System.Xml;

namespace Orid.Management.Statistics.Tools.Decomposition.Data
{
    class OrderFlowProgress:DataBase
    {
        #region 构造函数 sql 内容

        protected StringBuilder sqlText = new StringBuilder() ;

        public OrderFlowProgress()
        {   //JobFile
            sqlText.Append(" SELECT [OrderID],[Progress],[Flowset]  FROM @OrderFlowProgress with(NOLOCK)");//ReadPast
            sqlText.Append(" WHERE OrderID=@OrderID ");         
        }
        #endregion

        public virtual List<ModelProgress> GetOrderFlowProgress(Guid? orderid) { return null; }

        protected List<ModelProgress> GetOrderFlowProgress(Guid? orderid,   string connectString, string tableName)
        {
            try
            {
                List<ModelProgress> listModelProgress = new List<ModelProgress>();

                if (orderid == null)
                {
                    return null;
                }

                sqlText.Replace("@OrderFlowProgress", tableName);

                using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
                {
                    conn.Open();
                    System.Data.SqlClient.SqlParameter[] SqlParameter;
                    SqlParameter = new System.Data.SqlClient.SqlParameter[]
                                 {    
                                       new System.Data.SqlClient.SqlParameter("@OrderID",System.Data.SqlDbType.UniqueIdentifier),  
                                
                                 };

                    SqlParameter[0].Value = orderid; 


                    SqlDataReader reader = SqlHelper.ExecuteReader(conn, System.Data.CommandType.Text, sqlText.ToString(), SqlParameter);


                    while (reader.Read())
                    {
                        ModelProgress modelProgress = new ModelProgress();

                        if (reader["Progress"] != DBNull.Value)
                        {
                            string progressXML = reader["Progress"].ToString() ;
                            listModelProgress  = GetProgress(progressXML);
                        }
                    }

                    if (conn.State == System.Data.ConnectionState.Open)
                        conn.Close();

                    return listModelProgress;
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw (new Exception(ex.Message));
            }
        }

       
    }
}
